namespace SmartSoft.Controls
{
    using SmartSoft;
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Windows.Forms;

    public class TextDate : MaskedTextBox
    {
        private DateTimeFormatType ie_DateTimeFormat;
        private object oOldValue;

        public TextDate()
        {
            this.DateTimeFormat = DateTimeFormatType.Date;
            base.InsertKeyMode = InsertKeyMode.Overwrite;
            base.Validating += new CancelEventHandler(this.TextDate_Validating);
        }

        protected override void OnCreateControl()
        {
            this.Font = AppInstance.SystemFont;
            base.OnCreateControl();
        }

        protected override void OnGotFocus(EventArgs e)
        {
            this.OldValue = this.Value;
            base.SelectionStart = 0;
            base.OnGotFocus(e);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            return ((keyData == Keys.Space) || base.ProcessCmdKey(ref msg, keyData));
        }

        private string Replicated(int pi_Times, string ps_Replaced)
        {
            string str = "";
            for (int i = 1; i <= pi_Times; i++)
            {
                str = str + ps_Replaced;
            }
            return str;
        }

        private void TextDate_Validating(object sender, CancelEventArgs e)
        {
            if ((this.Text.TrimEnd(new char[0]) != this.NullDateTimeString.TrimEnd(new char[0])) && (this.Value == SDBNull.Value))
            {
                MessageBox.Show("Invalid date!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                e.Cancel = true;
            }
            else if (this.Value != DBNull.Value)
            {
                DateTime time = (DateTime) this.Value;
                if (time.Year < 0x76c)
                {
                    DateTime time2 = (DateTime) this.Value;
                    MessageBox.Show("Year must be bigger than " + time2.Year.ToString() + "!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    e.Cancel = true;
                }
            }
        }

        public DateTimeFormatType DateTimeFormat
        {
            get
            {
                return this.ie_DateTimeFormat;
            }
            set
            {
                string shortDatePattern = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
                string dateSeparator = DateTimeFormatInfo.CurrentInfo.DateSeparator;
                this.ie_DateTimeFormat = value;
                if (this.ie_DateTimeFormat == DateTimeFormatType.Date)
                {
                    base.Mask = this.Replicated(shortDatePattern.IndexOf(dateSeparator), "0") + dateSeparator + this.Replicated((shortDatePattern.LastIndexOf(dateSeparator) - shortDatePattern.IndexOf(dateSeparator)) - 1, "0") + dateSeparator + this.Replicated((shortDatePattern.Length - shortDatePattern.LastIndexOf(dateSeparator)) - 1, "0");
                }
                else if (this.ie_DateTimeFormat == DateTimeFormatType.Time)
                {
                    base.Mask = "00:00";
                }
                else
                {
                    base.Mask = this.Replicated(shortDatePattern.IndexOf(dateSeparator), "0") + dateSeparator + this.Replicated((shortDatePattern.LastIndexOf(dateSeparator) - shortDatePattern.IndexOf(dateSeparator)) - 1, "0") + dateSeparator + this.Replicated((shortDatePattern.Length - shortDatePattern.LastIndexOf(dateSeparator)) - 1, "0") + " 00:00";
                }
            }
        }

        private string NullDateTimeString
        {
            get
            {
                string shortDatePattern = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
                string dateSeparator = DateTimeFormatInfo.CurrentInfo.DateSeparator;
                if (this.DateTimeFormat == DateTimeFormatType.Date)
                {
                    return (this.Replicated(shortDatePattern.IndexOf(dateSeparator), " ") + dateSeparator + this.Replicated((shortDatePattern.LastIndexOf(dateSeparator) - shortDatePattern.IndexOf(dateSeparator)) - 1, " ") + dateSeparator + this.Replicated((shortDatePattern.Length - shortDatePattern.LastIndexOf(dateSeparator)) - 1, " "));
                }
                if (this.DateTimeFormat == DateTimeFormatType.Time)
                {
                    return "  :  ";
                }
                return (this.Replicated(shortDatePattern.IndexOf(dateSeparator), " ") + dateSeparator + this.Replicated((shortDatePattern.LastIndexOf(dateSeparator) - shortDatePattern.IndexOf(dateSeparator)) - 1, " ") + dateSeparator + this.Replicated((shortDatePattern.Length - shortDatePattern.LastIndexOf(dateSeparator)) - 1, " ") + "   :  ");
            }
        }

        [Browsable(false)]
        public object OldValue
        {
            get
            {
                return this.oOldValue;
            }
            set
            {
                if (this.oOldValue != value)
                {
                    this.oOldValue = value;
                }
            }
        }

        [Browsable(false), Bindable(true)]
        public object Value
        {
            get
            {
                if (this.Text == this.NullDateTimeString)
                {
                    return DBNull.Value;
                }
                try
                {
                    return Convert.ToDateTime(this.Text);
                }
                catch
                {
                    return DBNull.Value;
                }
            }
            set
            {
                if ((value == DBNull.Value) || (value == SDBNull.Value) || (((DateTime)value) == DateTime.MinValue))
                {
                    this.Text = this.NullDateTimeString;
                }
                else
                {
                    this.Text = ((DateTime) value).ToShortDateString();
                }
            }
        }

        [Category("DateTime settings"), RefreshProperties(RefreshProperties.All), Browsable(false), Description("Indicating value has been changed.")]
        public bool ValueChanged
        {
            get
            {
                return !this.OldValue.Equals(this.Value);
            }
        }

        public enum DateTimeFormatType
        {
            Date,
            Time,
            DateTime
        }
    }
}

